package com.miquan.minotehelper.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.miquan.minotehelper.R;
import com.miquan.minotehelper.utils.ImageUtils;
import com.miquan.minotehelper.utils.ShareUtils;
import com.miquan.minotehelper.utils.ToastUtils;
import com.miquan.minotehelper.view.ClipImageView;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class ClipImageActivity extends SuperActivity implements View.OnClickListener {
    private ClipImageView clip_image_view;
    private Button btn_share;

    private String mFilePath;

    public static void startInNewTask(Context context, String filePath) {
        Intent intent = new Intent(context, ClipImageActivity.class);
        intent.putExtra("file_path", filePath);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clip_image);

        readIntentData();
        initView();
    }

    private void readIntentData() {
        mFilePath = getIntent().getStringExtra("file_path");
    }

    private void initView() {
        clip_image_view = (ClipImageView) findViewById(R.id.clip_image_view);
        Bitmap bitmap = BitmapFactory.decodeFile(mFilePath);
        clip_image_view.setBitmap(bitmap);

        btn_share = (Button) findViewById(R.id.btn_share);
        btn_share.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_share:
                Bitmap bitmap = clip_image_view.getBitmap();

                String fileName = "" + new Date().getTime() + ".png";
                File file = new File("/storage/emulated/0/MINoteHelper/" + fileName);
                try {
                    ImageUtils.savePNG(bitmap, file);
                    ShareUtils.shareImage(context, file);
                    ToastUtils.showToast(context, "分享成功");
                } catch (IOException e) {
                    e.printStackTrace();
                    ToastUtils.showToast(context, "分享失败");
                } finally {
                    bitmap.recycle();
                }


                finish();
                break;
        }
    }
}
